package com.dawnling.countdownlistdemo.ui;

import android.util.Log;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dawnling.countdownlistdemo.R;
import com.dawnling.countdownlistdemo.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 订单列表适配器
 * Created by LXL on 2017/7/5.
 */
public class OrderAdater extends BaseQuickAdapter<OrderBean, BaseViewHolder> {

    //加入倒计时的数据集合
    public List<BaseViewHolder> listViewHolder = new ArrayList<>();

    public OrderAdater() {
        super(R.layout.item_order);
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderBean item) {
        if (item.delivery_countdown_time > 0){
            //倒计时时间
            if (!listViewHolder.contains(helper)) {
                listViewHolder.add(helper);
            }
            onCountDown(helper);
        }else {
            helper.setText(R.id.tvAppointmentDate, "00/00");
        }
    }

    /**
     * 显示倒计时
     */
    public void onNextSecond() {
        if (listViewHolder.isEmpty()) {
            return;
        }
        for (BaseViewHolder baseViewHolder : listViewHolder) {
            onCountDown(baseViewHolder);
        }
    }

    public void onCountDown(final BaseViewHolder viewHolder) {
        if (viewHolder.getAdapterPosition() == -1 || viewHolder.getAdapterPosition() >= getData().size()) {
            return;
        }
        final TextView tv_sendTime = viewHolder.getView(R.id.tvAppointmentDate);
        final OrderBean bean = getItem(viewHolder.getAdapterPosition());
        tv_sendTime.post(new Runnable() {
            @Override
            public void run() {
                String timeAll = TimeUtils.long2String3((bean.delivery_countdown_time));
                List<String> list= Arrays.asList(timeAll.split(":"));
                if (list.size() <= 2) tv_sendTime.setText("剩余时间：" + list.get(0) + "M/" + list.get(1) + "S\n个数：" + list.size());
                else tv_sendTime.setText("剩余时间：" + list.get(0) + "H/" + list.get(1) + "M/" + list.get(2) + "S\n个数：" + list.size());
                Log.e("时间", "时间" + bean.delivery_countdown_time);
            }
        });
    }
}
